﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CodeGenerator.Core;
using CodeGenerator.Common;

namespace CodeGenerator.UI
{
    public partial class FrmConfigSqlite : Form
    {
        public FrmConfigSqlite()
        {
            InitializeComponent();
        }
        #region Field
        /// <summary>
        /// 标识数据库配置信息添加是否成功
        /// </summary>
        public ServerInfo server=null;
        #endregion

        #region Action
        private void btnLink_Click(object sender, EventArgs e)
        {
            errorProvider1.Clear();
            if (string.IsNullOrEmpty(txt_ServerName.Text.Trim()))
            {
                errorProvider1.SetError(txt_ServerName, "服务器名不为空！");
                return;
            }

            if (string.IsNullOrEmpty(txt_Alias.Text.Trim()))
            {
                errorProvider1.SetError(txt_Pwd, "别名不为空！");
                return;
            }

            ServerInfo entity;
            string messages;
            bool result= TestConnection(out entity, out messages);
            if (result)
            {
                MessageUtil.ShowTips("测试连接服务器成功！！！");
            }
            else
            {
                MessageUtil.ShowWarning(messages);
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            errorProvider1.Clear();
            if (string.IsNullOrEmpty(txt_ServerName.Text.Trim()))
            {
                errorProvider1.SetError(txt_ServerName, "服务器名不为空！");
                return;
            }
            if (string.IsNullOrEmpty(txt_Alias.Text.Trim()))
            {
                errorProvider1.SetError(txt_Pwd, "别名不为空！");
                return;
            }

            ServerInfo entity;
            string messages;
            if (TestConnection(out entity, out messages))
            {
                string aFile = entity.ServerName;
                entity.DatabaseName = aFile.Substring(aFile.LastIndexOf("\\") + 1, (aFile.LastIndexOf(".") - aFile.LastIndexOf("\\") - 1));
                if (Config.AddServer(entity))
                {
                    server = entity; 
                    this.Close();
                }
                else
                {
                    MessageUtil.ShowWarning("SqlServer数据库信息配置失败！！！");
                }
            }
            else
            {
                MessageUtil.ShowWarning(messages);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion
        
        #region 私有方法
        /// <summary>
        /// 测试数据库连接
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="messages"></param>
        /// <param name="result"></param>
        private bool TestConnection(out ServerInfo entity, out string messages)
        {
            entity = new ServerInfo();
            entity.Alias = txt_Alias.Text.Trim();
            entity.DatabaseType=DatabaseType.Sqlite.ToString();
            entity.ServerName = txt_ServerName.Text.Trim();
            entity.ConnectString = Config.CreateConnectionString(entity,"",txt_Pwd.Text);
        

            return DBFactory.Database(DatabaseType.Sqlite).TestConnnect(entity, out messages);
        }
        #endregion
    }
}
